package com.wanghui.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wanghui.reggie.common.BaseContext;
import com.wanghui.reggie.common.R;
import com.wanghui.reggie.entity.AddressBook;
import com.wanghui.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 地址簿管理
 */
@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增
     *
     * @param addressBook
     */
    @PostMapping()
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
        log.info("addressBook：{}", addressBook);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 列表展示
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list() {
        //获取当前登录用户id
        Long currentId = BaseContext.getCurrentId();
        //根据当前登录用户id查询其地址信息
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        //id相等，isDelete字段为0，排序按照修改时间排序
        queryWrapper.eq(currentId != null, AddressBook::getUserId, currentId)
                .eq(AddressBook::getIsDeleted, 0)
                .orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 设置默认地址
     * 先将全部地址is_default字段设置为0，在将指定字段设置为1
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        log.info("address {}", addressBook);
        //将所有状态设置为0
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());   //比对id
        updateWrapper.set(AddressBook::getIsDefault, 0);
        addressBookService.update(updateWrapper);

        //根据id修改方式将制定地址状态设置为1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 修改地址_回显功能_通过id获取地址信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> get(@PathVariable Long id) {
        log.info("id {}", id);
        AddressBook address = addressBookService.getById(id);
        return R.success(address);
    }

    /**
     * 修改地址_修改功能_将数据写入数据库
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        log.info("addressBook {}", addressBook);
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getId, addressBook.getId())
                .eq(AddressBook::getUserId, addressBook.getUserId());
        addressBookService.update(addressBook, wrapper);
        return R.success("修改成功！");
    }

    /**
     * 修改地址_删除指定地址
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam Long ids) {
        log.info("ids {}", ids);
        //删除时，修改is_delete字段,0表示未删除，1表示删除
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(null != ids, AddressBook::getId, ids)
                .set(AddressBook::getIsDeleted, 1);
        addressBookService.update(wrapper);
        return R.success("删除成功！");
    }

    /**
     * 获取地址簿默认地址
     *
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if (addressBook == null) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }
}
